<?php
namespace app\common\service;

use app\common\logic\CartLogic;
use app\common\logic\GoodsLogic;
use think\Page;
use think\facade\Db;

class Article extends Base {
    static $AUTH_CHECK = false; //默认必须登录
    public function getartlist(){
        $page = input('pagenum');
        $limit = input('limit')?:15;
        $user_id = $this->user_id;

        $field = '*';
        $map = [];
        if(input('cid')){
            $map['cat_id'] = input('cid');
        }
        if(input('keyword')){
            $map['title'] = ['like','%'.input('keyword').'%'];
        }
        $lists = Db::name('article')
            ->where($map)
            ->order('id desc')
            ->field($field)
            ->paginate(15,false,['page'=>$page,'list_rows'=>$limit]);
        if($lists->total()){
            foreach($lists as $k=>$v){
                $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
                $lists->offsetSet($k,$v);
            }
        }
        $clist = Db::name('article_cat')->where('1=1')->select();
        $listtype = 0;//0横排 1双排 2滑动 3单排 4三排 5单排三图
        $set = ['title_size'=>12];
        $result = [
            'status' => true,
            'msg' => '',
            'datalist' => $lists->items(),
            'listtype' => $listtype,
            'clist' => $clist,
            'title' => '文章类别',
            'set' => $set
        ];
        return $result;
    }

    public function detail(){
        $id = input('id');
        $detail = Db::name('article')->where('id',$id)->find();
        if(empty($detail)){
            $result = [
                'status' => false,
                'msg' => '文章不存在'
            ];
            return $result;
        }
        $detail['add_time'] = date('Y-m-d H:i:s',$detail['add_time']);
        $detail['canpl'] = 0;
        $pagecontent = json_decode($detail['content'],true);
        $result = [
            'status' => true,
            'detail' => $detail,
            'pagecontent' => $pagecontent
        ];
        return $result;
    }
}
